﻿using System.Data;

namespace XD.Office
{
    /// <summary>
    /// DOC模版导出功能
    /// </summary>
    public class DocFileWriter
    {
        //模版参数
        private TemplateParams _params;

        //进度条
        public System.Windows.Forms.ProgressBar ProgressBar;

        /// <summary>
        /// XLS导出
        /// </summary>
        /// <param name="param">参数对象</param>
        public DocFileWriter(TemplateParams param)
        {
            _params = param;
        }

        /// <summary>
        /// XLS导出
        /// </summary>
        /// <param name="tempFile">模版配置文件</param>
        public DocFileWriter(string tempFile)
        {
            _params = TemplateParams.ReadFromFile(tempFile);
        }

        /// <summary>
        /// 导出到doc文件(多个地物合并导出到一个文档中)
        /// </summary>
        public void WriteSingleDoc(string outputFile, DataTable data)
        {
            DocTemplateExporter exportor = new DocTemplateExporter(_params, outputFile);
            exportor.ProgressBar = ProgressBar;
            exportor.ExportData(data);
            exportor.Save();
            exportor.Close();
        }

        /// <summary>
        /// 导出到doc文件(一个地物导出到一个文档中)
        /// </summary>
        public void WriteMultiDoc(string outputFolder, DataTable data)
        {
            //导出DataRow
            this.ProgressBar.Value = 0;
            ProgressBar.Maximum = data.Rows.Count;
            for (int i = 0; i < data.Rows.Count; i++)
            {
                string outputFile = System.IO.Path.Combine(outputFolder,
                    "export_" + data.Rows[i][0].ToString() + System.IO.Path.GetExtension(_params.TemplateFile));
                DocTemplateExporter exportor = new DocTemplateExporter(_params, outputFile);
                exportor.ProgressBar = ProgressBar;
                exportor.ExportDataCar(data.Rows[i]);
                exportor.Save();
                exportor.Close();
                ProgressBar.Value = i + 1;
            }
        }
    }
}
